﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AcsCommon;
using System.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using YiShi.Class;
using Microsoft.JScript;

namespace YiShi.Exam
{
    public partial class ExamListControl : System.Web.UI.UserControl
    {
        DbAccess db;
        string sqlTemplate = "select {0} from Exam {1} {2}";
        string trTemplate = @"
<tr style='display:{0}'>
    <td class='tdclass'>
        <input type='radio' name='Record' value='{1}'/>
    </td>
    <td class='tdclass' align='left'>
        {2}
    </td>
</tr>";
        protected string html;
        protected PageIndex pi;
        public int Width { get; set; }
        public int PerPageRecordCount { get; set; }
        public ExamListControl()
        {
            Width = 0;
            PerPageRecordCount = 10;

            db = DbAccess.Get("Admin");
            pi = new PageIndex();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            object jsonobj = null;
            try
            {
                string action = Request["ActionType"] == null ? "" : Request["ActionType"];
                switch (action.ToLower())
                {
                    case "jump":
                        jsonobj = Jump();
                        break;
                    default:
                        Query();
                        break;
                }
            }
            catch (Exception e1)
            {
                jsonobj = new
                {
                    IsSuccess = "false",
                    Message = e1.Message,
                    Source = e1.Source,
                    StackTrace = e1.StackTrace
                };
            }
            finally
            {
                if (jsonobj != null)
                {
                    Response.Write(JsonConvert.SerializeObject(jsonobj));
                    Response.End();
                }
            }
        }

        private object Jump()
        {
            string sql = String.Format(sqlTemplate, "count(Id) as Total", this.Request["Where"], "");
            pi.TotalRecordCount = (int)db.QueryValue(sql);
            pi.PerPageRecordCount = int.Parse(this.Request["PPRC"]);
            pi.JumpNo = int.Parse(this.Request["CPN"]);
            pi.Action = PageAction.Jump;
            pi.ExecuteAction();
            if (this.Request["PageAction"] != "Jump" && this.Request["PageAction"] != "Sort")
            {
                pi.Action = (PageAction)Enum.Parse(typeof(PageAction), this.Request["PageAction"]);
                pi.ExecuteAction();
            }

            sql = String.Format(sqlTemplate, "*", this.Request["Where"], "order by " + this.Request["OrderBy"]);
            DataTable dt = db.QueryDataTable("Table", sql, pi.GetStartIndex(), pi.PerPageRecordCount);
            string[] trs = new string[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                string name = dr.GetValue("Name");
                trs[i] = GlobalObject.escape(String.Format(trTemplate, "", dr.GetValue("Id"), name));
            }
            return new
                {
                    Trs = trs,
                    IsSuccess = "true",
                    CPN = pi.CurrentPageNo,
                    TPC = pi.TotalPageCount,
                    PPRC = pi.PerPageRecordCount,
                    CPRC = pi.CurrentPageRecordCount,
                    TRC = pi.TotalRecordCount
                };
        }

        private JsonObject Query()
        {
            string sql = String.Format(sqlTemplate, "count(Id) as Total", "", "");
            pi.TotalRecordCount = (int)db.QueryValue(sql);
            pi.PerPageRecordCount = PerPageRecordCount;

            sql = String.Format(sqlTemplate, "*", "", "order by id desc");
            DataTable dt = db.QueryDataTable("Table", sql, pi.GetStartIndex(), pi.PerPageRecordCount);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                string name = dr.GetValue("Name");
                html += String.Format(trTemplate, "", dr.GetValue("Id"), name);
            }

            return null;
        }
    }
}